<template>
  <div class="login-container" style="padding-top: 100px">

    <!-- 登录form -->
    <el-form
      v-if="!isShowRegist"
      autoComplete="on"
      :model="loginForm"
      :rules="loginRules"
      ref="loginForm"
      label-position="left"
      label-width="0px"
      class="card-box login-form">
      <h3 class="title">{{loginTitle}}</h3>
      <el-form-item prop="user">
        <span class="svg-container svg-container_login">
          <icon-svg icon-class="yonghuming" />
        </span>
        <el-input name="user" type="text" v-model="loginForm.user" autoComplete="on" :placeholder="inputPlaceholder" />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <icon-svg icon-class="mima" ></icon-svg>
        </span>
        <el-input name="password" :type="pwdType" @keyup.enter.native="handleLogin" v-model="loginForm.password" autoComplete="on"
                  placeholder="密码"></el-input>
        <span class='show-pwd' @click='showPwd'><icon-svg icon-class="eye" /></span>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" style="width:100%;" :loading="loading" @click.native.prevent="handleLogin">
          登录
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="text" style="width:100%;" :loading="loading" @click.native.prevent="showRegist">
          注册
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="text" style="width:100%;" @click='showDialog=true'>打开第三方登录</el-button>
      </el-form-item>
      <!--<div class='tips'>账号:admin 密码随便填</div>
      <div class='tips'>账号:editor  密码随便填</div>-->
    </el-form>
    <el-tabs
      v-if="!isShowRegist"
      v-model="activeName"
      @tab-click="handleClick"
      style="width: 500px;margin: 0 auto">
      <el-tab-pane label="手机登录" name="mobile"></el-tab-pane>
      <el-tab-pane label="邮箱登录" name="email"></el-tab-pane>
    </el-tabs>

    <!-- 注册form -->
    <el-form
      v-if="isShowRegist"
      autoComplete="on"
      :model="registerForm"
      :rules="loginRules"
      ref="registerForm"
      label-position="left"
      label-width="0px"
      class="card-box login-form">
      <h3 class="title">{{registerTitle}}</h3>
      <el-form-item prop="user">
        <span class="svg-container svg-container_login">
          <icon-svg icon-class="yonghuming" />
        </span>
        <el-input name="user" type="text" v-model="registerForm.user" autoComplete="on" :placeholder="inputPlaceholder" />
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <icon-svg icon-class="mima" ></icon-svg>
        </span>
        <el-input name="password" :type="pwdType" @keyup.enter.native="handleRegister" v-model="registerForm.password" autoComplete="on"
                  placeholder="密码"></el-input>
        <span class='show-pwd' @click='showPwd'><icon-svg icon-class="eye" /></span>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" style="width:100%;" :loading="loading" @click.native.prevent="handleRegister">
          注册
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="text" style="width:100%;" :loading="loading" @click.native.prevent="backLogin">
          返回登录
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="text" style="width:100%;" @click='showDialog=true'>打开第三方登录</el-button>
      </el-form-item>
      <!--<div class='tips'>账号:admin 密码随便填</div>
      <div class='tips'>账号:editor  密码随便填</div>-->
    </el-form>
    <el-tabs
      v-if="isShowRegist"
      v-model="activeName"
      @tab-click="handleRegisterClick"
      style="width: 500px;margin: 0 auto">
      <el-tab-pane label="手机注册" name="mobile"></el-tab-pane>
      <el-tab-pane label="邮箱注册" name="email"></el-tab-pane>
    </el-tabs>

    <el-dialog title="第三方验证" :visible.sync="showDialog">
      本地不能模拟，请结合自己业务进行模拟！！！<br/><br/><br/>
      邮箱登录成功,请选择第三方验证<br/>
      <social-sign />
    </el-dialog>

  </div>
</template>

<script>
import { isvalidUsername } from '@/utils/validate'
import { login,getSMSCode} from '@/api/login'
import socialSign from './socialsignin'

export default {
  components: { socialSign },
  name: 'login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!isvalidUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePass = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能小于6位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        user: '',
        password: '',
        identity_type:'mobile'
      },
      registerForm: {
        client_type:1,
        user: '',
        password: '',
        regist_type:'mobile'
      },
      loginRules: {
        user: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePass }]
      },
      loading: false,
      activeName:'mobile',
      loginTitle:'手机号登录',
      inputPlaceholder:'手机号',
      registerTitle:'手机号注册',
      showDialog: false,
      isShowRegist:false,
      pwdType: 'password',
    }
  },
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('Login', this.loginForm).then(() => {
            this.loading = false;
            this.$router.push({ path: '/' });
          }).catch(() => {
            this.loading = false
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    handleClick(tab) {
      if(tab.index == '0'){
        this.loginForm.identity_type = 'mobile'
        this.activeName = 'mobile'
        this.loginTitle = '手机号登录'
        this.inputPlaceholder = '手机号'
      }else if(tab.index == '1'){
        this.loginForm.identity_type = 'email'
        this.activeName = 'email'
        this.loginTitle = '邮箱号登录'
        this.inputPlaceholder = '邮箱号'
      }
    },
    showRegist(){
      this.isShowRegist = true
      this.activeName = 'mobile'
      this.pwdType = 'password'
    },
    handleRegister(){

    },
    handleRegisterClick(tab){
      if(tab.index == '0'){
        this.registerForm.regist_type = 'mobile'
        this.activeName = 'mobile'
        this.registerTitle = '手机号注册'
        this.inputPlaceholder = '手机号'
      }else if(tab.index == '1'){
        this.registerForm.regist_type = 'email'
        this.activeName = 'email'
        this.registerTitle = '邮箱号注册'
        this.inputPlaceholder = '邮箱号'
      }
    },
    backLogin(){
      this.isShowRegist = false
      this.activeName = 'mobile'
      this.pwdType = 'password'
    },
    showPwd() {
      if (this.pwdType === 'password') {
        this.pwdType = ''
      } else {
        this.pwdType = 'password'
      }
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
  @import "src/styles/mixin.scss";
  $bg:#2d3a4b;
  $dark_gray:#889aa4;
  $light_gray:#eee;

  .login-container {
    @include relative;
    height: 100vh;
    background-color: $bg;
    input:-webkit-autofill {
      -webkit-box-shadow: 0 0 0px 1000px #293444 inset !important;
      -webkit-text-fill-color: #fff !important;
    }
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
    }
    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;
    }
    .tips {
      font-size: 14px;
      color: #fff;
      margin-bottom: 10px;
    }
    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
      &_login {
        font-size: 20px;
      }
    }
    .title {
      font-size: 26px;
      font-weight: 400;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
    .login-form {
      position: absolute;
      left: 0;
      right: 0;
      width: 400px;
      padding: 35px 35px 15px 35px;
      margin: 50px auto;
    }
    .el-form-item {
      border: 1px solid rgba(255, 255, 255, 0.1);
      background: rgba(0, 0, 0, 0.1);
      border-radius: 5px;
      color: #454545;
    }
    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
    }
    .thirdparty-button{
      position: absolute;
      right: 35px;
      bottom: 28px;
    }
  }
</style>
